package cn.zhouchao.basic.controller;

import cn.zhouchao.basic.util.AjaxResult;
import cn.zhouchao.basic.util.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;

/**
 * 定义：分布式存储
 * fastdfs 接口
 */
@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {
    @PostMapping("/upload") // 图片上传
    public AjaxResult upload(@RequestPart("file") MultipartFile file) throws IOException {
        // 真实路径
        String originalFilename = file.getOriginalFilename();
        // 获取扩展名 org.apache.commons.io.FilenameUtils;
        String extension = FilenameUtils.getExtension(originalFilename);
        // 执行上传
        String path = FastdfsUtil.upload(file.getBytes(), extension);
        return AjaxResult.me().success(path);
    }

    /**
     * 删除
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path) throws IOException {
        String allPath = path.substring(1);
        String groupName = allPath.substring(0, allPath.indexOf("/"));
        String pathName = allPath.substring(allPath.indexOf("/") + 1);
        FastdfsUtil.delete(groupName, pathName);
        return AjaxResult.me().success(path);
    }
}
